import k
import tkinter as tk
import load_coeffs
import interp
import math
import numpy as np
'''
STEP1. format displacement and fea model info
put odb file in 'step1-odb-convert', then call .bat file 'extraing-element.bat' 
and 'extraing-displacement.bat'.
 and csv files are obtained like ‘tc01.odb.elements_nodes.csv’ and '' in 'step1-odb-convert/result' folder.
'''

import tkinter as tk
from tkinter import filedialog
import os
import csv

def choose_csv_files():
    # Create a Tkinter window
    root = tk.Tk()
    # Hide the main window
    root.withdraw()
    
    # Get the directory of the current script
    script_directory = os.path.dirname(__file__)
    
    # Pop up a file dialog to let the user choose the first CSV file
    file_path1 = filedialog.askopenfilename(
        title="Select the element-node CSV file",
        filetypes=[("CSV files", "*.csv")],
        initialdir=script_directory+"\\step1-odb-convert\\result"
    )
    if not file_path1:
        print("No element-node CSV file selected")
        return None, None
    
    # Pop up a file dialog to let the user choose the second CSV file
    file_path2 = filedialog.askopenfilename(
        title="Select the displacement CSV file",
        filetypes=[("CSV files", "*.csv")],
        initialdir=script_directory+"\\step1-odb-convert\\result"
    )
    if not file_path2:
        print("No displacement CSV file selected")
        return file_path1, None
    
    # Return the absolute paths of the two CSV files
    return file_path1, file_path2

def save_csv(data,file_path):
    x_transposed = list(map(list, zip(*data)))
    with open(file_path, mode="w", newline="", encoding="utf-8") as file:
        writer = csv.writer(file)  
        for col in x_transposed:
            writer.writerow(col)
            writer.writerow(col)

    print(f"write data to {file_path}")

# Test the function
if __name__ == "__main__":

    root  = os.getcwd()

    #1.before startup, run abaqus script to get element-node and displacement files in folder ‘step1-odb-convert'
    #2.choose element-node and displacement
    element_node,disp= choose_csv_files()
    print(f"Path of the element-node selected CSV file: {element_node}")
    print(f"Path of the displacement selected CSV file: {disp}")


    #3. set up crack angle
    angle=math.pi/2
    dir=[math.sin(math.pi/2-angle),math.cos(math.pi/2-angle),0]

    #4. interp displacement
    a=1 #crack size
    E=192000 #YOUNG'S MODULE OF MOEL
    crack_center=[0,0,0] #CRACK CENTER
    r_ratio_range=[0.4,0.05]
    r_range=[a/r_ratio_range[0],a/r_ratio_range[1]]
    r=np.linspace(r_range[0],r_range[1],10)
    r=r.tolist()

    bc_disp_files,status_okisp=interp.interp([[disp,element_node]],[r],100,crack_center,dir)
    #bc_disp_files=["tc01.odb_"]
    result=[]
    for i in range(len(bc_disp_files)):
        result.append(k.run_edge_crack_2d(root+"\\interp_result\\"+bc_disp_files[i]+".csv",2*a,E))

    print(result)
    save_csv(result[0],"predicted-sif.csv")



